package com.efast.dmom.aps.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.aps.bean.Bom;
import com.efast.dmom.aps.bean.BomItem;
import com.efast.dmom.aps.bean.BomTree;
import com.efast.dmom.aps.bean.Part;
import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.datahandlers.BomHandler;
import com.efast.dmom.aps.datahandlers.BomItemHandler;
import com.efast.dmom.aps.datahandlers.PartHandler;
import com.efast.dmom.aps.entity.BomEntity;
import com.efast.dmom.aps.entity.BomItemEntity;
import com.efast.dmom.aps.service.BomService;

import jodd.util.StringUtil;

@Service
public class BomServiceImpl implements BomService {
	@Autowired
	private BomHandler bomHandler;

	@Autowired
	private BomItemHandler bomItemHandler;

	@Autowired
	private PartHandler PartHandler;

	@Override
	public ResultBean save(Bom bom) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (bom.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(bom.getInnerData().getName())
				|| !StringUtil.isNotBlank(bom.getInnerData().getRevision())
				|| !StringUtil.isNotBlank(bom.getInnerData().getType())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查BOM名称、版本、类型是否填写!");
			return resultBean;
		}
		Bom bomByBomNoAndRevision = bomHandler.getByNameAndRevision(bom.getInnerData().getName(),
				bom.getInnerData().getRevision());
		if (bomByBomNoAndRevision.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该BOM已存在!");
			return resultBean;
		}
		bomHandler.save(bom);
		resultBean.setSuccess(true);
		resultBean.setMessage("BOM保存成功!");
		return resultBean;
	}

	@Override
	public ResultBean update(Bom bom) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (bom.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(bom.getInnerData().getName())
				|| !StringUtil.isNotBlank(bom.getInnerData().getRevision())
				|| !StringUtil.isNotBlank(bom.getInnerData().getType())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查BOM名称、版本、类型是否填写!");
			return resultBean;
		}

		Bom bomById = bomHandler.getById(bom.getInnerData().getId());
		if (bomById.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该BOM不存在!");
			return resultBean;
		}
		if (!bom.getInnerData().getName().equals(bomById.getInnerData().getName())
				&& !bom.getInnerData().getRevision().equals(bomById.getInnerData().getRevision())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("BOM名称和BOM版本不能修改!");
			return resultBean;
		}
		bomHandler.update(bom);
		resultBean.setSuccess(true);
		resultBean.setMessage("BOM修改成功!");
		return resultBean;
	}

	@Override
	public ResultBean delete(Bom bom) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (bom.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		List<Part> partList = PartHandler.getPartByBomNameAndRevision(bom.getInnerData().getName(),
				bom.getInnerData().getRevision());
		if (partList != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该Bom被物料引用，不能删除!");
			return resultBean;
		}
		bomHandler.delete(bom);
		BomItem bomItem = new BomItem();
		BomItemEntity entity = new BomItemEntity();
		bomItem.setInnerData(entity);
		bomItem.getInnerData().setBomId(bom.getInnerData().getId());
		bomItemHandler.deleteByBom(bomItem);
		resultBean.setSuccess(true);
		resultBean.setMessage("BOM删除成功!");
		return resultBean;
	}

	@Override
	public Bom getById(long id) {
		Bom bom = bomHandler.getById(id);
		if (bom.getInnerData() == null) {
			return null;
		}
		List<BomItem> bomItemList = bomItemHandler.getByBomId(id);
		if (bomItemList != null) {
			bom.setItems(bomItemList);
		}
		return bom;
	}

	@Override
	public Bom getByName(String name) {
		Bom bom = bomHandler.getByName(name);
		if (bom.getInnerData() == null) {
			return null;
		}
		List<BomItem> bomItemList = bomItemHandler.getByBomId(bom.getInnerData().getId());
		if (bomItemList != null) {
			bom.setItems(bomItemList);
		}

		return bom;
	}

	@Override
	public Bom getBomByNameAndRevision(String bomName, String bomRevision) {
		Bom bom = bomHandler.getByNameAndRevision(bomName, bomRevision);
		if (bom.getInnerData() == null) {
			return null;
		}
		List<BomItem> bomItemList = bomItemHandler.getByBomId(bom.getInnerData().getId());
		if (bomItemList != null) {
			bom.setItems(bomItemList);
		}
		return bom;
	}

	@Override
	public List<Bom> getAllBom() {
		List<Bom> bomList = bomHandler.getAllBom();
		if (bomList == null) {
			return null;
		}
//		if (bomList != null) {
//			for (Bom data : bomList) {
//				List<BomItem> bomItemList = bomItemHandler.getByBomId(data.getInnerData().getId());
//				if (bomItemList != null) {
//					data.setItems(bomItemList);
//				}
//			}
//		}
		return bomList;
	}

	@Override
	public BomTree getAllBomByCondition(Bom bom) {
		if (bom.getLength() == 0) {
			bom.setLength(10);
		}
		if (bom.getStart() == 1) {
			bom.setStart(0);
		} else if(bom.getStart() > 1){
			bom.setStart((bom.getStart()-1)*bom.getLength());
		}
		List<BomEntity> bomList = bomHandler.getAllBomAndBomItem(bom);
		bom.setStart(0);
		bom.setLength(0);
		List<BomEntity> bomSize = bomHandler.getAllBomAndBomItem(bom);
		BomTree bomTree = new BomTree();
		if (bomList != null && bomSize !=null) {
		bomTree.setCountSize(bomSize.size());
		bomTree.setBomList(bomList);
		} 
		return bomTree;
	}

}
